<template>
  <el-dialog :title="title" :visible.sync="openDistribution" width="500px" append-to-body>
    <el-form class="dialog-form" label-suffix="：" ref="form" :model="form" :rules="rules" label-width="120px"
             :style="{'--form-item-width': '100%'}"
    >
      <el-form-item label="用户信息" prop="userIds">
        <el-select v-model="form.userIds" placeholder="选择用户信息"
                   multiple
                   clearable size="small">
          <el-option
            v-for="dict in this.teamUserList"
            :key="dict.userId"
            :label="dict.nickName + '（' + dict.deptName + '）'"
            :value="dict.userId"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { teamUserList } from "@/api/teamCustomer/teamCustomer";
import { pusDis } from "@/api/customer/customer";

export default {
  name: 'TransferCustomer',
  data() {
    return {
      loading: false,
      ids: [],
      // 弹出层标题
      title: '客户分配',
      // 是否显示弹出层
      openDistribution: false,
      type: "",
      // 表单参数
      form: {
        // userId: undefined,
        userIds: []
      },
      operationType: '',
      teamUserList: [],
      teamCustomerList: [],
      // 表单校验
      rules: {
        // userId: [
        //   {required: true, message: '用户不能为空', trigger: 'change'}
        // ],
        userIds: [
          {
            required: true,
            message: '用户不能为空',
            trigger: ['blur', 'change'],
          },
        ]
      }
    }
  },
  methods: {
    getTeamUserList() {
      teamUserList().then(res => {
        if (res.code == 200) {
          this.teamUserList = res.data;
        }
      })
    },
    /** 打开弹窗 */
    openDialog(ids, operationType) {
      this.ids = ids;
      this.operationType = operationType;
      this.openDistribution = true
      this.getTeamUserList();
      this.resetForm('form')
    },
    /** 取消按钮 */
    cancel() {
      this.openDistribution = false
      //if (this.$parent.getList) {
      //  this.$parent.getList();
      //}
      //if (this.$parent.$parent.refreshList) {
      //  console.log("二级弹框取消getList刷新了")
      //  this.$parent.$parent.refreshList();
      //}
    },

    /** 确定按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const userIds = this.form.userIds;
          const usersEnd = [];
          userIds.forEach(item => {
            const obj = this.teamUserList.find(it => it.userId == item);
            const data = {
              userId: obj.userId,
              nickName: obj.nickName,
              userDeptId: obj.deptId
            }
            usersEnd.push(data);
          })
          if (this.operationType == 'pushCustomer') {
            const data = {
              disCustomerBos: this.ids,
              userBos: usersEnd
            };
            pusDis(data).then(() => {
              this.$modal.msgSuccess("分配成功");
              this.openDistribution = false
              if (this.$parent.getList) {
                this.$parent.getList();
              }
            })
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
